package com.yykj.user.center.server.service.impl;

import com.yykj.common.enums.ResultEnums;
import com.yykj.common.exception.NormalException;
import com.yykj.common.util.LocalStringUtil;
import com.yykj.user.center.client.form.AddGroupForm;
import com.yykj.user.center.server.dto.GroupPatientDTO;
import com.yykj.user.center.server.dto.GroupPatientDetailDTO;
import com.yykj.user.center.server.form.AddRelationGroupPatient;
import com.yykj.user.center.server.form.RemoveGroupForm;
import com.yykj.user.center.server.form.RemovePatientForm;
import com.yykj.user.center.server.form.UpdateGroupForm;
import com.yykj.user.center.server.model.*;
import com.yykj.user.center.server.repository.GroupRepository;
import com.yykj.user.center.server.repository.RelationGroupPatientRepository;
import com.yykj.user.center.server.repository.patient.ConsultationInfoRepository;
import com.yykj.user.center.server.repository.patient.PatientCardInfoRepository;
import com.yykj.user.center.server.repository.patient.PatientRepository;
import com.yykj.user.center.server.service.GroupService;
import com.yykj.user.center.server.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class GroupServiceImpl implements GroupService {
    @Autowired
    private GroupRepository groupRepository;
    @Autowired
    private RelationGroupPatientRepository relationGroupPatientRepository;
    @Autowired
    private ConsultationInfoRepository consultationInfoRepository;
    @Autowired
    private PatientCardInfoRepository patientCardInfoRepository;
    @Autowired
    private PatientRepository patientRepository;
    @Override
    public Boolean create(AddGroupForm addGroupForm) {
        if (StringUtils.isBlank(addGroupForm.getName())) {
            throw new NormalException(ResultEnums.NO_GROUP_ERROR);
        }
        List<Group> groups = groupRepository.findAllByNameAndTypeAndHospitalIdAndDoctorIdAndState(addGroupForm.getName(), addGroupForm.getType(), addGroupForm.getHospitalId(), addGroupForm.getDoctorId(), true);
        if (groups.size() > 0) {
            throw new NormalException(ResultEnums.EXITS_GROUP_ERROR);
        }
        Group group = new Group();
        BeanUtils.copyProperties(addGroupForm, group);
        group.setId(LocalStringUtil.getUUID());
        group.setState(true);
        groupRepository.save(group);
        return true;
    }

    @Override
    public void createDefault(AddGroupForm addGroupForm) {
        addGroupForm.setName("默认分组");
        List<Group> groups = groupRepository.findAllByNameAndTypeAndHospitalIdAndDoctorIdAndState(addGroupForm.getName(), addGroupForm.getType(), addGroupForm.getHospitalId(), addGroupForm.getDoctorId(), true);
        if (groups.size() > 0) {
            throw new NormalException(ResultEnums.EXITS_GROUP_ERROR);
        }
        Group group = new Group();
        BeanUtils.copyProperties(addGroupForm, group);
        group.setId(LocalStringUtil.getUUID());
        group.setState(true);
        group.setIsDefault(true);
        groupRepository.save(group);
    }

    @Override
    public void update(UpdateGroupForm updateGroupForm) {
        Group group = groupRepository.findById(updateGroupForm.getId()).orElse(null);
        if (group != null) {
            Boolean isDefault = group.getIsDefault();
            if (isDefault != null && isDefault) {
                throw new NormalException(ResultEnums.NO_UPDATE_DEFAULT_ERROR);
            }
            List<Group> groups = groupRepository.findAllByNameAndTypeAndHospitalIdAndDoctorIdAndState(updateGroupForm.getName(), group.getType(), updateGroupForm.getHospitalId(), updateGroupForm.getDoctorId(), true);
            if (groups.size() > 0) {
                throw new NormalException(ResultEnums.EXITS_GROUP_ERROR);
            }
            BeanUtils.copyProperties(updateGroupForm, group);
            groupRepository.save(group);
        }
    }

    @Override
    public Boolean buildRelation(AddRelationGroupPatient addRelationGroupPatient) {
        if (addRelationGroupPatient.getType() == 1) {
            buildGroupRelation(addRelationGroupPatient);
        } else if (addRelationGroupPatient.getType() == 2) {
            buildLableRelation(addRelationGroupPatient);
        }
        return true;
    }

    @Override
    @Transactional
    public Boolean removePatientFromGroup(RemovePatientForm removePatientForm) {
        Integer type = removePatientForm.getType();
        String patientCardId = removePatientForm.getPatientCardId();
        String groupId = removePatientForm.getGroupId();
        if (type == 1) {
            String doctorId = removePatientForm.getDoctorId();
            Integer hospitalId = removePatientForm.getHospitalId();
            RelationGroupPatient relationGroupPatient = relationGroupPatientRepository.findFirstByGroupIdAndPatientCardId(groupId, patientCardId);
            Group group = groupRepository.findFirstByHospitalIdAndDoctorIdAndIsDefaultAndState(hospitalId, doctorId, true, true);
            relationGroupPatientRepository.deleteById(relationGroupPatient.getId());
            RelationGroupPatient relation = new RelationGroupPatient();
            relation.setId(LocalStringUtil.getUUID());
            relation.setPatientCardId(patientCardId);
            relation.setHospitalId(hospitalId);
            relation.setGroupId(group.getId());
            relation.setDoctorId(doctorId);
            relation.setType(1);
            relationGroupPatientRepository.save(relation);
        } else {
//            RelationGroupPatient relationGroupPatient = relationGroupPatientRepository.findFirstByGroupIdAndPatientCardId(groupId, patientCardId);
            relationGroupPatientRepository.deleteByGroupIdAndPatientCardId(groupId, patientCardId);
        }

        return true;
    }
    static ArrayList<Integer> state = new ArrayList<>();
    static {
        state.add(1);
        state.add(2);
        state.add(3);
    }

    @Override
    public List<GroupPatientDTO> queryByGroup(Integer hospitalId, String doctorId, Integer type) {
//        int[] orderTypes = {11,12,13};
//        List<ConsultationInfoOrder> consultationInfoOrders = consultationInfoRepository.findAllByDoctorIdAndOrderTypeInAndPayStatus(doctorId,orderTypes,1);
        List<GroupPatientDTO> groupPatientDTOS = new ArrayList<>();
        List<Group> groups = groupRepository.findAllByDoctorIdAndTypeAndStateOrderByCreateTimeDesc(doctorId, type, true);
        for (Group group : groups) {
            GroupPatientDTO groupPatientDTO = new GroupPatientDTO();
            groupPatientDTO.setGroupId(group.getId());
            groupPatientDTO.setType(group.getType());
            groupPatientDTO.setIsDefault(group.getIsDefault());
            groupPatientDTO.setName(group.getName());
            List<RelationGroupPatient> relationGroupPatients = relationGroupPatientRepository.findAllByGroupId(group.getId());
            List<String> patientCardIds = relationGroupPatients.stream().map(RelationGroupPatient::getPatientCardId).collect(Collectors.toList());
            List<GroupPatientDetailDTO> groupPatientDetailDTOS = new ArrayList<>();
            List<ConsultationInfoOrder> consultationInfoOrders = new ArrayList<>();
            if(patientCardIds.size() > 0){
                consultationInfoOrders = consultationInfoRepository.findAllByPatientCardIdInAndStateIn(patientCardIds, state);
            }
//            consultationInfoOrders = consultationInfoRepository.findAllByPatientCardIdInAndState(patientCardIds, 1);
            groupPatientDTO.setNum(consultationInfoOrders.size());

            for (ConsultationInfoOrder consultationInfoOrder : consultationInfoOrders) {
                GroupPatientDetailDTO groupPatientDetailDTO = new GroupPatientDetailDTO();
                groupPatientDetailDTO.setPatientName(consultationInfoOrder.getPatientName());
                groupPatientDetailDTO.setConsultationId(consultationInfoOrder.getId());
                List<String> labels = new ArrayList<>();
                List<RelationGroupPatient> relationGroupPatientList = relationGroupPatientRepository.findAllByPatientCardIdAndType(consultationInfoOrder.getPatientCardId(), 2);
                for (RelationGroupPatient relationGroupPatient : relationGroupPatientList) {
                    String label = relationGroupPatient.getName();
                    labels.add(label);
                }
                Optional<PatientCardInfo> patientCardInfoz = this.patientCardInfoRepository.findById(consultationInfoOrder.getPatientCardId());
                if(patientCardInfoz.isPresent()){
                    PatientCardInfo patientCardInfo = patientCardInfoz.get();
                    groupPatientDetailDTO.setSex(patientCardInfo.getSex());
                    groupPatientDetailDTO.setAge(StringUtil.getAge(patientCardInfo.getBirthday()));
                    Optional<Patient> patientz = this.patientRepository.findById(patientCardInfo.getAccountId());
                    if(patientz.isPresent()){
                        groupPatientDetailDTO.setFaceUrl(patientz.get().getAvatar());
                        groupPatientDetailDTO.setTencentAccountId(patientz.get().getTencentAccountId());
                    }

                }
                groupPatientDetailDTO.setLables(labels);
                groupPatientDetailDTO.setPatientId(consultationInfoOrder.getPayAccountId());
                groupPatientDetailDTO.setPatientCardId(consultationInfoOrder.getPatientCardId());
                groupPatientDetailDTOS.add(groupPatientDetailDTO);

                /*for (RelationGroupPatient relationGroupPatient : relationGroupPatients) {
                    List<String> labels = new ArrayList<>();
                    String label = relationGroupPatient.getName();
                    labels.add(label);
                    GroupPatientDetailDTO groupPatientDetailDTO = new GroupPatientDetailDTO();
                    BeanUtils.copyProperties(relationGroupPatient, groupPatientDetailDTO);
                    groupPatientDetailDTO.setLables(labels);

                    groupPatientDetailDTO.setPatientId(relationGroupPatient.getPatientCardId());
                    groupPatientDetailDTO.setPatientCardId(relationGroupPatient.getPatientCardId());
                    groupPatientDetailDTOS.add(groupPatientDetailDTO);
                }
                groupPatientDTO.setNum(relationGroupPatients.size());
                groupPatientDTO.setGroupPatientDetailDTOList(groupPatientDetailDTOS);
                groupPatientDTOS.add(groupPatientDTO);*/
            }
            groupPatientDTO.setGroupPatientDetailDTOList(groupPatientDetailDTOS);
            groupPatientDTOS.add(groupPatientDTO);
        }
        return groupPatientDTOS;
    }

    @Override
    public List<GroupPatientDetailDTO> queryAllPatientByDoctorId(String doctorId) {
        List<ConsultationInfoOrder> consultationInfoOrders = consultationInfoRepository.findAllByDoctorIdAndState(doctorId, 1);
        List<GroupPatientDetailDTO> groupPatientDetailDTOS = new ArrayList<>();
        for (ConsultationInfoOrder consultationInfoOrder : consultationInfoOrders) {
            GroupPatientDetailDTO groupPatientDetailDTO = new GroupPatientDetailDTO();
            groupPatientDetailDTO.setPatientName(consultationInfoOrder.getPatientName());
            groupPatientDetailDTO.setConsultationId(consultationInfoOrder.getId());
            List<String> labels = new ArrayList<>();
            List<RelationGroupPatient> relationGroupPatientList = relationGroupPatientRepository.findAllByPatientCardIdAndType(consultationInfoOrder.getPatientCardId(), 2);
            for (RelationGroupPatient relationGroupPatient : relationGroupPatientList) {
                String label = relationGroupPatient.getName();
                labels.add(label);
            }
            groupPatientDetailDTO.setLables(labels);
            groupPatientDetailDTOS.add(groupPatientDetailDTO);
        }
        return groupPatientDetailDTOS;
    }

    @Override
    public List<GroupPatientDetailDTO> availableGroupPatients(String doctorId, Integer hospitalId) {
        Group group = groupRepository.findFirstByHospitalIdAndDoctorIdAndIsDefaultAndState(hospitalId, doctorId, true, true);
        List<RelationGroupPatient> relationGroupPatients = relationGroupPatientRepository.findAllByGroupId(group.getId());
        List<String> cardIds = relationGroupPatients.stream().map(RelationGroupPatient::getPatientCardId).collect(Collectors.toList());
        List<ConsultationInfoOrder> consultationInfoOrders = consultationInfoRepository.findAllByPatientCardIdInAndState(cardIds, 1);
        List<GroupPatientDetailDTO> groupPatientDetailDTOS = new ArrayList<>();
        for (ConsultationInfoOrder consultationInfoOrder : consultationInfoOrders) {
            GroupPatientDetailDTO groupPatientDetailDTO = new GroupPatientDetailDTO();
            groupPatientDetailDTO.setPatientName(consultationInfoOrder.getPatientName());
            groupPatientDetailDTO.setConsultationId(consultationInfoOrder.getId());
            List<String> labels = new ArrayList<>();
            List<RelationGroupPatient> relationGroupPatientList = relationGroupPatientRepository.findAllByPatientCardIdAndType(consultationInfoOrder.getPatientCardId(), 2);
            for (RelationGroupPatient relationGroupPatient : relationGroupPatientList) {
                String label = relationGroupPatient.getName();
                labels.add(label);
            }
            groupPatientDetailDTO.setLables(labels);
            groupPatientDetailDTOS.add(groupPatientDetailDTO);
        }
        return groupPatientDetailDTOS;
    }

    @Override
    public List<GroupPatientDetailDTO> availableLabelPatients(String doctorId, Integer hospitalId, String labelId) {
        List<RelationGroupPatient> relationGroupPatients = relationGroupPatientRepository.findAllByGroupId(labelId);
        List<String> cardIds = relationGroupPatients.stream().map(RelationGroupPatient::getPatientCardId).collect(Collectors.toList());
        List<ConsultationInfoOrder> consultationInfoOrders = consultationInfoRepository.findAllByPatientCardIdNotInAndState(cardIds, 1);
        List<GroupPatientDetailDTO> groupPatientDetailDTOS = new ArrayList<>();
        for (ConsultationInfoOrder consultationInfoOrder : consultationInfoOrders) {
            GroupPatientDetailDTO groupPatientDetailDTO = new GroupPatientDetailDTO();
            groupPatientDetailDTO.setPatientName(consultationInfoOrder.getPatientName());
            groupPatientDetailDTO.setConsultationId(consultationInfoOrder.getId());
            List<String> labels = new ArrayList<>();
            List<RelationGroupPatient> relationGroupPatientList = relationGroupPatientRepository.findAllByPatientCardIdAndType(consultationInfoOrder.getPatientCardId(), 2);
            for (RelationGroupPatient relationGroupPatient : relationGroupPatientList) {
                String label = relationGroupPatient.getName();
                labels.add(label);
            }
            groupPatientDetailDTO.setLables(labels);
            groupPatientDetailDTOS.add(groupPatientDetailDTO);
        }
        return groupPatientDetailDTOS;
    }

    @Override
    public List<GroupPatientDTO> list(String doctorId, Integer hospitalId, Integer type, String name) {
        List<ConsultationInfoOrder> consultationInfoOrders = new ArrayList<>();
        List<GroupPatientDTO> groupPatientDTOS = new ArrayList<>();
        if (type == 2) {
            List<Group> groups = groupRepository.findAll(new Specification<Group>() {
                @Override
                public Predicate toPredicate(Root<Group> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                    List<Predicate> predicateList = new ArrayList<>();
                    predicateList.add(builder.equal(root.get("doctorId"), doctorId));
                    predicateList.add(builder.equal(root.get("hospitalId"), hospitalId));
                    predicateList.add(builder.equal(root.get("type"), type));
                    if (StringUtils.isNotBlank(name)) {
                        predicateList.add(builder.like(root.get("name"), "%"+name+"%"));
                    }
                    Predicate[] predicates = new Predicate[predicateList.size()];
                    return query.where(predicateList.toArray(predicates)).getRestriction();
                }
            }, Sort.by(Sort.Order.asc("createTime")));
            for(Group group:groups){
                List<RelationGroupPatient> relationGroupPatients = relationGroupPatientRepository.findAllByGroupId(group.getId());
                GroupPatientDTO groupPatientDTO = new GroupPatientDTO();
                groupPatientDTO.setGroupId(group.getId());
                groupPatientDTO.setName(group.getName());
                groupPatientDTO.setNum(relationGroupPatients.size());
                for (RelationGroupPatient relationGroupPatient : relationGroupPatients) {
                    String groupId = relationGroupPatient.getGroupId();
//                    List<RelationGroupPatient> relationGroupPatientList = relationGroupPatientRepository.findAllByGroupId(groupId);
                    List<String> patientCardIds = relationGroupPatients.stream().map(RelationGroupPatient::getPatientCardId).collect(Collectors.toList());
                    consultationInfoOrders = consultationInfoRepository.findAllByPatientCardIdInAndState(patientCardIds, 1);
                    List<GroupPatientDetailDTO> groupPatientDetailDTOS = new ArrayList<>();
                    for (ConsultationInfoOrder consultationInfoOrder : consultationInfoOrders) {
                        GroupPatientDetailDTO groupPatientDetailDTO = new GroupPatientDetailDTO();
                        groupPatientDetailDTO.setPatientName(consultationInfoOrder.getPatientName());
                        groupPatientDetailDTO.setConsultationId(consultationInfoOrder.getId());
                        groupPatientDetailDTO.setPatientCardId(consultationInfoOrder.getPatientCardId());
                        List<String> labels = new ArrayList<>();
                        List<RelationGroupPatient> relations = relationGroupPatientRepository.findAllByPatientCardIdAndType(consultationInfoOrder.getPatientCardId(), 2);
                        for (RelationGroupPatient relation : relations) {
                            String label = relation.getName();
                            labels.add(label);
                        }
                        groupPatientDetailDTO.setLables(labels);
                        groupPatientDetailDTOS.add(groupPatientDetailDTO);
                    }
                    groupPatientDTO.setGroupPatientDetailDTOList(groupPatientDetailDTOS);
                }
                groupPatientDTOS.add(groupPatientDTO);
            }

        } else if (type == 1) {
            List<RelationGroupPatient> relationGroupPatients = relationGroupPatientRepository.findAll(new Specification<RelationGroupPatient>() {
                @Override
                public Predicate toPredicate(Root<RelationGroupPatient> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                    List<Predicate> predicateList = new ArrayList<>();
                    predicateList.add(builder.equal(root.get("doctorId"), doctorId));
                    predicateList.add(builder.equal(root.get("hospitalId"), hospitalId));
                    predicateList.add(builder.equal(root.get("type"), type));
                    if (StringUtils.isNotBlank(name)) {
                        predicateList.add(builder.like(root.get("patientName"), "%"+name+"%"));
                    }
                    Predicate[] predicates = new Predicate[predicateList.size()];
                    return query.where(predicateList.toArray(predicates)).getRestriction();
                }
            }, Sort.by(Sort.Order.asc("createTime")));
            Set set = new HashSet<>();
            List<String> groupIds = relationGroupPatients.stream().map(RelationGroupPatient::getGroupId).collect(Collectors.toList());
            List<String> ids = new ArrayList<>();
            groupIds.stream().forEach(groupId -> {
                if (set.add(groupId)) {
                    ids.add(groupId);
                }
            });
            for (String groupId : ids) {
                Group group = groupRepository.findById(groupId).orElse(null);
                if (group != null) {
                    GroupPatientDTO groupPatientDTO = new GroupPatientDTO();
                    groupPatientDTO.setGroupId(groupId);
                    groupPatientDTO.setName(group.getName());
                    List<String> cardIds = new ArrayList<>();
                    for (RelationGroupPatient relationGroupPatient : relationGroupPatients) {
                        cardIds.add(relationGroupPatient.getPatientCardId());
                    }
                    groupPatientDTO.setNum(cardIds.size());
                    consultationInfoOrders = consultationInfoRepository.findAllByPatientCardIdInAndStateIn(cardIds, state);
                    List<GroupPatientDetailDTO> groupPatientDetailDTOS = new ArrayList<>();
                    for (ConsultationInfoOrder consultationInfoOrder : consultationInfoOrders) {
                        GroupPatientDetailDTO groupPatientDetailDTO = new GroupPatientDetailDTO();
                        groupPatientDetailDTO.setPatientName(consultationInfoOrder.getPatientName());
                        groupPatientDetailDTO.setConsultationId(consultationInfoOrder.getId());
                        List<String> labels = new ArrayList<>();
                        List<RelationGroupPatient> relations = relationGroupPatientRepository.findAllByPatientCardIdAndType(consultationInfoOrder.getPatientCardId(), 2);
                        for (RelationGroupPatient relation : relations) {
                            String label = relation.getName();
                            labels.add(label);
                        }
                        groupPatientDetailDTO.setLables(labels);
                        groupPatientDetailDTO.setPatientCardId(consultationInfoOrder.getPatientCardId());
                        groupPatientDetailDTOS.add(groupPatientDetailDTO);
                    }
                    groupPatientDTO.setGroupPatientDetailDTOList(groupPatientDetailDTOS);
                    groupPatientDTOS.add(groupPatientDTO);
                }

            }
        }

        return groupPatientDTOS;
    }

    @Override
    public Boolean removeGroup(RemoveGroupForm removeGroupForm) {
        Integer type = removeGroupForm.getType();
        String groupId = removeGroupForm.getId();
        List<RelationGroupPatient> relationGroupPatients = relationGroupPatientRepository.findAllByGroupId(groupId);
        if (type == 1) {
            Group group = groupRepository.findById(groupId).orElse(null);
            if (group != null) {
                if (group.getIsDefault()!=null) {
                    if (group.getIsDefault()) {
                        throw new NormalException(ResultEnums.NO_DELETE_DEFAULT_ERROR);
                    }
                }
                String doctorId = group.getDoctorId();
                Integer hospitalId = group.getHospitalId();
                Group defaultGroup = groupRepository.findFirstByHospitalIdAndDoctorIdAndIsDefaultAndState(hospitalId, doctorId, true, true);
                for (RelationGroupPatient relation : relationGroupPatients) {
                    RelationGroupPatient relationGroupPatient = new RelationGroupPatient();
                    relationGroupPatient.setGroupId(defaultGroup.getId());
                    relationGroupPatient.setPatientName(relation.getPatientName());
                    relationGroupPatient.setPatientCardId(relation.getPatientCardId());
                    relationGroupPatient.setName(defaultGroup.getName());
                    relationGroupPatient.setType(defaultGroup.getType());
                    relationGroupPatient.setHospitalId(relation.getHospitalId());
                    relationGroupPatient.setId(LocalStringUtil.getUUID());
                    relationGroupPatient.setDoctorId(relation.getDoctorId());
                    relationGroupPatient.setAge(relation.getAge());
                    relationGroupPatient.setRemarks(relation.getRemarks());
                    relationGroupPatient.setSex(relation.getSex());
                    relationGroupPatientRepository.save(relationGroupPatient);
                }
                relationGroupPatientRepository.deleteInBatch(relationGroupPatients);
                groupRepository.deleteById(groupId);

            }
        } else if (type == 2) {
            relationGroupPatientRepository.deleteInBatch(relationGroupPatients);
            groupRepository.deleteById(groupId);
        }
        return true;
    }

    public void buildGroupRelation(AddRelationGroupPatient addRelationGroupPatient) {
        //首先解除该患者与其他分组的关系，然后再建立关系
        //首先删除
        List<RelationGroupPatient> relationGroupPatients = relationGroupPatientRepository.findAllByDoctorIdAndPatientCardIdInAndType(addRelationGroupPatient.getDoctorId(), addRelationGroupPatient.getPatientCardIds(), addRelationGroupPatient.getType());
        for (RelationGroupPatient relationGroupPatient : relationGroupPatients) {
            relationGroupPatientRepository.deleteById(relationGroupPatient.getId());
        }
        //建立关系
        List<String> patientCardIds = addRelationGroupPatient.getPatientCardIds();
        for (String cardId : patientCardIds) {
            Group group = groupRepository.findById(addRelationGroupPatient.getGroupId()).orElse(null);
            RelationGroupPatient relationGroupPatient = new RelationGroupPatient();
            PatientCardInfo patientCardInfo = patientCardInfoRepository.findById(cardId).orElse(null);
            relationGroupPatient.setPatientCardId(cardId);
            relationGroupPatient.setPatientName(patientCardInfo.getPatientName());
            relationGroupPatient.setName(group.getName());
            relationGroupPatient.setHospitalId(addRelationGroupPatient.getHospitalId());
            relationGroupPatient.setGroupId(addRelationGroupPatient.getGroupId());
            relationGroupPatient.setDoctorId(addRelationGroupPatient.getDoctorId());
            relationGroupPatient.setType(addRelationGroupPatient.getType());
            relationGroupPatient.setId(LocalStringUtil.getUUID());
            relationGroupPatientRepository.save(relationGroupPatient);
        }

    }

    public void buildLableRelation(AddRelationGroupPatient addRelationGroupPatient) {
        //建立关系
        List<String> patientCardIds = addRelationGroupPatient.getPatientCardIds();
        for (String cardId : patientCardIds) {
            Group group = groupRepository.findById(addRelationGroupPatient.getGroupId()).orElse(null);
            PatientCardInfo patientCardInfo = patientCardInfoRepository.findById(cardId).orElse(null);
            RelationGroupPatient relationGroupPatient = new RelationGroupPatient();
            relationGroupPatient.setName(group.getName());
            relationGroupPatient.setPatientName(patientCardInfo.getPatientName());
            relationGroupPatient.setPatientCardId(cardId);
            relationGroupPatient.setHospitalId(addRelationGroupPatient.getHospitalId());
            relationGroupPatient.setGroupId(addRelationGroupPatient.getGroupId());
            relationGroupPatient.setDoctorId(addRelationGroupPatient.getDoctorId());
            relationGroupPatient.setType(addRelationGroupPatient.getType());
            relationGroupPatient.setId(LocalStringUtil.getUUID());
            relationGroupPatientRepository.save(relationGroupPatient);
        }
    }
}
